/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.maslyk.invitation.entitybean;

import java.math.BigDecimal;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.maslyk.invitation.audit.ObjectName;
import com.maslyk.invitation.audit.PropertyLabel;
import com.maslyk.invitation.entitybean.abstrct.GenericEntity;
import com.maslyk.invitation.enums.OrderPositionTypeEnum;

/**
 *
 * @author kris
 */
@Entity
@Table(name = "wigniette")
@ObjectName(label="Winietka")
public class Vigniette extends GenericEntity implements IOrderPosition
{
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;
    @Column(name = "totalcount")
    @PropertyLabel(label="Ilość")
    private int totalcount;
    @Column(name = "overPrintCount")
    @PropertyLabel(label="Ilość z nadrukiem")
    private int overPrintCount;
    @Column(name = "code")
    @PropertyLabel(label="Kod")
    private String code;
    @Column(name = "unitprice")
    @PropertyLabel(label="Cena jednostkowa")
    private BigDecimal unitprice = new BigDecimal(0.0);
    @Column(name = "note")
    @PropertyLabel(label="Uwagi")
    private String note;
    @JoinColumn(name = "orderid", referencedColumnName = "id")
    @ManyToOne(optional = false, fetch = FetchType.LAZY, targetEntity=Order.class)
    private Order order;

    @Transient
    private StringBuffer calculatingNote = new StringBuffer();

    public Vigniette()
    {
    }

    public Vigniette(Integer id)
    {
        this.id = id;
    }

    public Vigniette(Integer id, int totalcount)
    {
        this.id = id;
        this.totalcount = totalcount;
    }

    public Integer getId()
    {
        return id;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }

    public int getTotalcount()
    {
        return totalcount;
    }

    public void setTotalcount(int totalcount)
    {
        this.totalcount = totalcount;
    }

    public String getCode()
    {
        return code;
    }

    public void setCode(String code)
    {
        this.code = code;
    }

    public BigDecimal getUnitprice()
    {
        return unitprice;
    }

    public void setUnitprice(BigDecimal unitprice)
    {
        this.unitprice = unitprice;
    }

    public Order getOrder()
    {
        return order;
    }

    public void setOrder(Order order)
    {
        this.order = order;
    }

    public String getNote()
    {
        return note;
    }

    public void setNote(String note)
    {
        this.note = note;
    }

    public int getOverPrintCount()
    {
        return overPrintCount;
    }

    public void setOverPrintCount(int overPrintCount)
    {
        this.overPrintCount = overPrintCount;
    }

    @Override
    public int hashCode()
    {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    public BigDecimal getAmount()
    {
        double totalAmount = 0.0d;
        calculatingNote = new StringBuffer();

        totalAmount += totalcount * unitprice.doubleValue();
        calculatingNote.append("Kwota za winietki: "+totalcount+" * "+unitprice.doubleValue()+" = "+totalAmount+"<br>");
        totalAmount += overPrintCount * 0.70;
        calculatingNote.append("Kwota za nadruk: "+overPrintCount+" * 0.70<br>");
        BigDecimal amount = new BigDecimal(totalAmount);
        amount.setScale(2,BigDecimal.ROUND_HALF_UP);
        return amount;
    }

    public StringBuffer getCalculatingNote()
    {
        return calculatingNote;
    }

    public OrderPositionTypeEnum getType()
    {
        return OrderPositionTypeEnum.WIGNIETTE;
    }

	public String getKey() {
		return getOrder().getNumber();
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		Vigniette v = (Vigniette) super.clone();
		v.setId(null);
		v.setOrder(null);
		return v;
	}
}
